package com.ln.yygh.hosp.controller.api;

import com.alibaba.excel.util.StringUtils;

import com.ln.yygh.vo.hosp.*;
import io.swagger.models.auth.In;
import org.springframework.data.domain.Page;
import com.ln.yygh.common.exception.YyghException;
import com.ln.yygh.common.helper.HttpRequestHelper;
import com.ln.yygh.common.result.*;
import com.ln.yygh.hosp.service.*;
import com.ln.yygh.model.hosp.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: lianXiaoChen
 * @create: 2021-05-29 14:17
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private Hospitalervice hospitalervice;
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //删除拍板
    @ApiOperation("删除排班")
    @PostMapping("/schedule/remove")
    public Result scheduleRemove(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());//讲发送来的信息转换为paramMap
        //签名鉴权
        boolean flag = hospitalervice.getSign(paramMap);
        if (!flag) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        if (StringUtils.isEmpty(hosScheduleId)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();

    }

    //上传排班
    @ApiOperation("上传排班")
    @PostMapping("/saveSchedule")
    public Result scheduleSave(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());//讲发送来的信息转换为paramMap
        //签名鉴权
        boolean flag = hospitalervice.getSign(paramMap);
        if (!flag) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.saveSchedule(paramMap);
        return Result.ok();
    }

    //查询排班列表
    @ApiOperation("查询排班列表")
    @PostMapping("/schedule/list")
    public Result scheduleList(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());//获取参数
        /**
         * 开始鉴权
         * 医院编号
         */
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //科室编号
        String depcode = (String) paramMap.get("depcode");
        /**
         * 我们在service自己写的鉴权签名
         */
        boolean flag = hospitalervice.getSign(paramMap);
        if (!flag) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        /**
         * 获取其他的参数
         */
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        /**
         * 开始去service
         */
        Page<Schedule> pageModel = scheduleService.getSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);

    }

    /**
     * 数据过来的的格式以及怎么获取
     *
     * @param request
     * @return
     */
    @PostMapping("/saveHospital")
    @ApiOperation("上传医院")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String) paramMap.get("logoData");
        if (!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            paramMap.put("logoData", logoData);
        }
        String sign = (String) paramMap.get("sign");
        System.out.println("sign = " + sign);
        boolean flag = hospitalervice.getSign(paramMap);
        if (!flag) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //讲信息转化为实体类后拿着医院的编码去医院设置表中拿到平台里面的签名拿出，
        // 然后在按照约定好的加密规则进行加密 md5  然后进行比较 ==  通过  ！= 不通过
        hospitalervice.saveHospital(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) throws YyghException {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        boolean flag = hospitalervice.getSign(paramMap);//自己写的鉴权
        if (!flag) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital hospital = hospitalervice.getHospitalByHoscode(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        /**
         * 开始鉴权
         */
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        /**
         * 我们在service自己写的鉴权签名
         */
        boolean flag = hospitalervice.getSign(paramMap);
        if (!flag) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.saveDepartment(paramMap);

        return Result.ok();
    }

    @ApiOperation(value = "查询课时列表")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        /**
         * 开始鉴权
         */
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        /**
         * 我们在service自己写的鉴权签名
         */
        boolean flag = hospitalervice.getSign(paramMap);
        if (!flag) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        /**
         * 获取其他的参数
         */
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));
        String depcode = (String) paramMap.get("depcode");
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);

        /**
         * 开始去service
         */
        Page<Department> pageModel = departmentService.getDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);


    }

    @ApiOperation(value = "删除科室")
    @PostMapping("/department/remove")
    public Result departmentRemove(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取到医院编号以及科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        //签名校验
        boolean flag = hospitalervice.getSign(paramMap);
        if (!flag) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.departmentRemove(hoscode, depcode);
        return Result.ok();
    }

}

